package com.xiaoshuidi.cloud.module.contract.job;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.framework.tenant.core.job.TenantJob;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.bill.OrderFeesDO;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.bill.RentOrderDO;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.OrderLateFeeVo;
import com.xiaoshuidi.cloud.module.contract.dal.mysql.bill.OrderFeesMapper;
import com.xiaoshuidi.cloud.module.contract.dal.mysql.bill.RentOrderMapper;
import com.xiaoshuidi.cloud.module.contract.dal.mysql.contract.OrderLateFeeMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
@RequiredArgsConstructor
@TenantJob
public class ContractBillBreakJob {

    @Resource
    private RentOrderMapper rentOrderMapper;
    @Resource
    private OrderLateFeeMapper orderLateFeeMapper;
    @Resource
    private OrderFeesMapper orderfeesMapper;

    @XxlJob("ContractBillBreakJob")
    @TenantIgnore
    public void execute() {
        log.info("ContractBillBreakJob逾期账单生成违约金开始");
        QueryWrapper<RentOrderDO> wrapper = Wrappers.query();
        wrapper.eq("pay_status","unpaid");
        wrapper.eq("deleted","0");
        wrapper.ge("deadline_pay_time", DateUtil.today());
        List<RentOrderDO> rentOrderDOS = rentOrderMapper.selectList(wrapper);

        //查询是否存在逾期账单
        if (CollectionUtil.isNotEmpty(rentOrderDOS)) {
            //查询配置
            List<OrderLateFeeVo> orderLateFeeVos = orderLateFeeMapper.selectList();
            Map<Long, List<OrderLateFeeVo>> collect = orderLateFeeVos.stream().collect(Collectors.groupingBy(OrderLateFeeVo::getApartmentId,Collectors.toList()));
            //遍历逾期账单
            rentOrderDOS.forEach(item->{
                Long apartmentId = item.getApartmentId();
                List<OrderLateFeeVo> orderLateFeeVosList = collect.get(apartmentId);
                //是否存在配置
                if(CollectionUtil.isNotEmpty(orderLateFeeVosList)){
                    //查看开关配置
                    OrderLateFeeVo orderLateFeeVo = orderLateFeeVosList.get(0);
                    Integer liquidatedDamages = orderLateFeeVo.getLiquidatedDamages();
                    //是否开启
                    if (((orderLateFeeVo.getEnterprise() == 1 && item.getRoommateId() == 1) || orderLateFeeVo.getEnterprise() == 0) && liquidatedDamages != 0) {
                        BigDecimal subtract = item.getTotalAmount().subtract(item.getPaidRentUtilities());
                        //查看 已付金额是否等于总金额
                        if(subtract.compareTo(new BigDecimal("0")) > 0 ){
                            //生成逾期账单
                            OrderFeesDO orderFeesDO = new OrderFeesDO();
                            orderFeesDO.setOrderId(item.getId());
                            orderFeesDO.setPayStatus("unpaid");
                            orderFeesDO.setFlowType(0);
                            orderFeesDO.setPaidAmount(new BigDecimal(0));
                            orderFeesDO.setFeeMonth(new BigDecimal(0));
                            orderFeesDO.setFeeName("逾期费用");
                            orderFeesDO.setFeeId(17L);
                            orderFeesDO.setStartTime(LocalDate.now());
                            orderFeesDO.setEndTime(LocalDate.now());
                            orderFeesDO.setDeptId(item.getDeptId());
                            orderFeesDO.setMemberId(item.getMemberId());
                            orderFeesDO.setOperatorName(item.getOperatorName());
                            //查看支扣款方式
                            if(liquidatedDamages == 1)
                                orderFeesDO.setAmount(new BigDecimal(orderLateFeeVo.getDefaultAmount()));
                            else
                                orderFeesDO.setAmount(subtract.multiply(new BigDecimal(orderLateFeeVo.getDefaultRatio()).divide(new BigDecimal(10000))).setScale(2,RoundingMode.HALF_UP));
                            orderfeesMapper.insert(orderFeesDO);
                        }
                    }
                }
            });
        }
    }
}
